<template>
    <div class="goodsList page">
        <formHeader :title="'商品列表'"></formHeader>
        <cube-scroll
                :data="goodsList"
                :options="options"
                ref="scroll"
                @pulling-up="onPullingUp">
            <toplist :items="goodsList" :saleType="Number(saleType)"></toplist>
        </cube-scroll>
        <div v-if="flag" @click="turn('/point/sign')" class="skipTo">
            <img src="./qiandao2.png"/>
        </div>
    </div>
</template>

<script>
    import toplist from '@/components/top-list'
    import {getSearchGoodsList,getHeadLineList,topList,getSearchList} from '@/api/home'
    import formHeader from '@/components/form-header'
    import {showLoading,hide} from "@/toast"
    export default {
        name : "index",
        components:{toplist,formHeader},
        data(){
            return{
                type:0,
                saleType:0,
                page:1,
                goodsList:[],
                options: {
                    pullUpLoad: true,
                },
                ways:'',
                name:'',
                flag:false
            }
        },
        activated(){
            showLoading('正在加载')
            this.flag = false
            this.goodsList=[]
            this.page = 1
            this.type=this.$route.query.type
            this.saleType=this.$route.query.saletype
            this.ways = this.$route.query.ways
            this.name = this.$route.query.name
            this.$refs.scroll.scrollTo(0,0,0)
            // console.log(this.saleType)
            // console.log(this.type)
            this.onPullingUp()


        },
        methods:{
            async getSearchGoodsList(){
                const {data:res} = await getSearchGoodsList(this.page,this.type,this.saleType)
                // console.log(res)
                if(this.saleType==4){
                    this.flag = true
                }
                if(this.page==1){
                    this.goodsList = res
                }else {
                    this.goodsList= this.goodsList.concat(res)
                }
                this.page ++
                hide()
            },
            async getHeadLineList(){
                const {data:res} = await getHeadLineList(this.saleType,this.page)
                if(this.page==1){
                    this.goodsList = res
                }else {
                    this.goodsList= this.goodsList.concat(res)
                }
                this.page ++
                hide()
            },
            async topList(){
                topList(this.page).then(res => {
                    if(this.page==1){
                        this.goodsList = res.data
                    }else {
                        this.goodsList= this.goodsList.concat(res.data)
                    }
                    this.page ++
                    hide()
                })
            },
            onPullingUp(){
                if(this.ways=='getmore'){
                    topList(this.page,this.type).then(res => {
                        if(this.page==1){
                            this.goodsList = res.data
                        }else {
                            this.goodsList= this.goodsList.concat(res.data)
                        }
                        this.page ++
                        hide()
                    })
                }else if(this.ways=='search'){
                    getSearchList(this.name,this.page,6).then(res=>{
                        if(this.page==1){
                            this.goodsList = res.data
                        }else {
                            this.goodsList= this.goodsList.concat(res.data)
                        }
                        this.page ++
                        hide()
                    })
                }else {
                    if(this.saleType==undefined&&this.type==undefined){
                        this.topList()
                    }else if(this.type==undefined){
                        this.getHeadLineList()
                    }else {
                        this.getSearchGoodsList()
                    }
                }
            },
            turn(path){
                this.$router.push({
                    path:path
                })
            }
        }
    }
</script>

<style scoped lang="stylus">
.goodsList
    position relative
    height 100%
    background-color $page-bg
    /deep/.top-list
        margin-top  1rem
.skipTo
    position fixed
    right 0.21rem
    bottom 3.80rem
    z-index 999
    img
        height 1.28rem
        width 1.28rem
</style>
